//
//  TCAVehiculeView.m
//  TCoreAnimation
//
//  Created by Louis CHU on 09/09/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "QuartzCore/QuartzCore.h"

#import "TCAVehiculeView.h"

#import "TCAVehicule.h"
#import "TCAVector2d.h"


#define distancef(point1,point2)      (sqrtf((point1.x-point2.x)*(point1.x-point2.x) + (point1.y-point2.y)*(point1.y-point2.y)))


@implementation TCAVehiculeView
@synthesize vehicule;
@synthesize destinationLocation;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.vehicule = [[TCAVehicule alloc] init];
        self.vehicule.velocity = 1;
        
        //TCAVector2d* vetor = [[TCAVector2d initWi
        TCAVector2d* vector = [[TCAVector2d alloc] initWithBegin: CGPointMake(0, 0) end: CGPointMake(1, 1)];
        self.vehicule.direction = vector;
        
        [self rotateDirection];
        
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    self.vehicule = nil;
    
    [super dealloc];
}


- (void) animationDidStart:(CAAnimation *)anim
{
    NSLog(@"animation did start");
    //[[self.animatedObject layer] setPosition:CGPointMake(50, 50)];
}

- (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"animation did stop %d ",flag);
    //[[self.animatedObject layer] setPosition:self.destinationLocation];
    
    //if (anim == [[self layer] animationForKey:@"tranformation"]) 
    //{
    NSString * keyPath = [((CABasicAnimation*)anim ) keyPath];
    if ([keyPath isEqualToString:@"position"]) 
    {
        if (flag) 
        {
            [[self layer] setPosition:self.destinationLocation];
            [self rotateDirection];
        }
    }
    else if ([keyPath isEqualToString:@"transform.rotation"]) 
    {
        //if (flag)   [[self layer] setPosition:self.destinationLocation];
    }
}

- (void) rotateDirection
{
    CABasicAnimation * rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    
   // float distance = distancef(currentPosition, aDestionationLocation);
    
    
    [rotation setDuration:0.3];
    //[mover setFromValue:[NSValue valueWithCGPoint:CGPointMake(0, 0)]];
    [rotation setToValue:[NSNumber numberWithFloat:3.14*0.5]];
    rotation.delegate =self;
    
    CAMediaTimingFunction * tf =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [rotation setTimingFunction:tf]; 
    
    [[self layer] addAnimation:rotation forKey:@"rotation"];

}

- (void) moveTo: (CGPoint) aDestionationLocation
{
    CGPoint currentPosition = [[[self layer] presentationLayer] position];
    NSLog(@"current position of object %f %f",currentPosition.x, currentPosition.y);
    [[self layer] removeAllAnimations];
    [[self layer] setPosition:currentPosition];
    
    self.destinationLocation = aDestionationLocation;
    CABasicAnimation * mover = [CABasicAnimation animationWithKeyPath:@"position"];
    
    
    //float distance = distancef(currentPosition, aDestionationLocation);
    
    
    [mover setDuration:1];
    //[mover setFromValue:[NSValue valueWithCGPoint:CGPointMake(0, 0)]];
    [mover setToValue:[NSValue valueWithCGPoint:self.destinationLocation]];
    mover.delegate =self;
    
    CAMediaTimingFunction * tf =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [mover setTimingFunction:tf]; 
    
    [[self layer] addAnimation:mover forKey:@"tranformation"];
}

- (TCAVector2d *) currentDirection
{
    return [[TCAVector2d alloc] initWithBegin:CGPointMake(self.frame.origin.x,
                                                          self.frame.origin.y + self.frame.size.height) 
                                          end:[self center]];
}

@end